package com.shf.controller;

import com.github.pagehelper.PageInfo;
import com.shf.entity.Role;
import com.shf.entity.Users;
import com.shf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 *用户控制层
 * @author 叶枫
 */
@RolesAllowed("ROLE_ADMIN")
@Controller
@RequestMapping("/admin/user/")
public class UserController {
    @Autowired
    @Qualifier("us")
    private UserService userService;

    /**
     * 添加用户
     * @param
     * @return
     */

    @RequestMapping("save")
    public ModelAndView userSave(Users users){
        users.setId(UUID.randomUUID().toString());
        System.out.println(users);
        ModelAndView mv = new ModelAndView();
        //调用业务层方法
        userService.saveUser(users);
        //响应r
       // return "user/user-add";
        mv.setViewName("user/user-add");
        return mv;
    }

    /**
     * 显示所有用户
     * @param page
     * @param size
     * @param mv
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView UserFindAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size,ModelAndView mv){
        //调用业务层方法
       PageInfo<Users> pageInfo = userService.UserFindAll(page,size);
       //响应
       mv.addObject("pageInfo",pageInfo);
        mv.setViewName("user/user-list");
        return mv;
    }

    /**
     * 展示详情
     *
     */
    @RequestMapping("findById")
    public ModelAndView UserDetail(@RequestParam("id") Integer id,ModelAndView mv){

        //调用业务层方法
       Users users = userService.UserDetail(id);
        List<Role> roles = users.getRoles();
        System.out.println(roles);

        //响应
        mv.addObject("user",users);
        mv.setViewName("user/user-show");
        return mv;
    }

    /**
     * 添加权限角色之前的回显
     * @param id
     * @param mv
     * @return
     */
    @RequestMapping("findUserByIdAndAllRole")
    public ModelAndView findUserByIdAndAllRole(@RequestParam("id") Integer id,ModelAndView mv){

        //调用业务层方法

       Users users = userService.findUserById(id);
       List<Role> roleList = userService.findAllRole();

       mv.addObject("user",users);
       mv.addObject("roleList",roleList);
        //响应
        mv.setViewName("user/user-role-add");
        return mv;
    }

    /**
     * 添加角色
     * @param req
     * @return
     */
    @RequestMapping("addRoleToUser")
    public String addRoleToUser(String userId, String  ids [],HttpServletRequest req){
        //调用业务层方法
        userService.addRoleToUser(userId,ids);

        return "redirect:/admin/user/findAll.do?page=1&size=5";

    }

    /***
     *删除用户
     */
    @RequestMapping("deleteUser")
    public String deleteUser(String id){
        //调用业务层方法
        userService.deleteUser(id);

        return "redirect:/admin/user/findAll.do?page=1&size=5";
    }
@RequestMapping("user-adds")
    public  String userAdd(){
        return "user/user-add";
    }
}
